import { lk_buyer,lk_common,lk_expert,lk_seller,lk_manager,lk_consumer } from '@/config/myConfig.js';
import storage from "@/utils/storage.js"
// 对网络请求重新封装，等待登陆后请求，防止token失败
function request(method, url, data, header) {
	return new Promise((resolve, reject) => {
		if (getApp().globalData._IsLogin_) {
			// console.warn("直接登陆："+url);
			requestX(method, url, data, header).then(function(data) {
				resolve(data);
			}).catch(function(data) {
				reject(data);
			})
		} else {
			// 设置定时器，等待登陆完成
			let cnt = 0;
			let tid = setInterval(() => {
				if (cnt > 2000) { //设置最大等待时间
					clearInterval(tid);
					reject("请求超时，请刷新重试！");
					return;
				}
				// console.warn("等待登陆完成："+(++cnt));
				if (getApp().globalData._IsLogin_) {
					// console.warn("等待登陆成功："+cnt+" ---"+url);
					clearInterval(tid);
					requestX(method, url, data, header).then(function(data) {
						resolve(data);
					}).catch(function(data) {
						reject(data);
					})
				}
			}, 10)
		}
	})
}

function requestX(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'Authorization': uni.getStorageSync('token'),
		'Authorization_jbs': uni.getStorageSync('token')
	}, header)
	return new Promise((resolve, reject) => {
		// uni.hideLoading();
		uni.request({
			url,
			method,
			data,
			header: headers,
			success: ({
				data: res
			}) => {
				//  uni.hideLoading() 
				if (res && res.code.code === 200) {
					try {
						const _data_ = JSON.parse(res.data || '{}');
						resolve(_data_);
					} catch (error) {
						resolve(res.data);
					}
				} else {
					uni.showToast({
						title: res.message || '服务器响应超时,请稍后再试！',
						icon: 'none'
					});
					reject(res.message);
				}
			},
			fail: function(res) {
				console.log(res, 9999)
				uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res.data);
			}
		});
	})
}

function requesthttpLogin(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
	}, header)
	return new Promise((resolve, reject) => {
		uni.hideLoading();
		uni.request({
			url,
			method,
			data,
			header: headers,
			success: ({
				data: res
			}) => {
				uni.hideLoading();
				resolve(res);
			},
			fail: function(res) {
				uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res);
			}
		});
	})
}

function requesthttp(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'accessToken': storage.getAccessToken(),
		'refreshToken': storage.getRefreshToken(),
		// 'Authorization': uni.getStorageSync('token'),
		// 'Authorization_jbs': uni.getStorageSync('token'),
		// 'accessToken':null,
	}, header)
	return new Promise((resolve, reject) => {
		uni.hideLoading();
		uni.request({
			url,
			method,
			data,
			header: headers,
			success: ({
				data: res
			}) => {
				uni.hideLoading();
				resolve(res);
			},
			fail: function(res) {
				uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res);
			}
		});
	})
}

function requestlist(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'Authorization':storage.getAccessToken(),
		'Authorization_jbs': storage.getAccessToken()
	}, header)
	return new Promise((resolve, reject) => {
		if (getApp().globalData._IsLogin_) {
			// console.warn("直接登陆："+url);
			requestlistX(method, url, data, header).then(function(data) {
				resolve(data);
			}).catch(function(data) {
				reject(data);
			})
		} else {
			// 设置定时器，等待登陆完成
			let cnt = 0;
			let tid = setInterval(() => {
				if (cnt > 500) { //设置最大等待时间
					clearInterval(tid);
					reject("请求超时，请刷新重试！");
					return;
				}
				// console.warn("等待登陆完成："+(++cnt));
				if (getApp().globalData._IsLogin_) {
					// console.warn("等待登陆成功："+cnt+" ---"+url);
					clearInterval(tid);
					requestlistX(method, url, data, header).then(function(data) {
						resolve(data);
					}).catch(function(data) {
						reject(data);
					})
				}
			}, 10)
		}
	})
}

function requestlistX(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'Authorization': storage.getAccessToken(),
		'Authorization_jbs': storage.getAccessToken()
	}, header)
	return new Promise((resolve, reject) => {
		// uni.hideLoading();
		uni.request({
			url,
			method,
			data,
			header: headers,
			success(res) {
				// uni.hideLoading();
				resolve(res.data);
			},
			fail: function(res) {
				// uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res);
			}
		});
	})
}

function requestRight(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'Authorization': storage.getAccessToken(),
		'Authorization_jbs': storage.getAccessToken(),
		'accessToken': storage.getAccessToken(),
	}, header)
	return new Promise((resolve, reject) => {
		// uni.hideLoading();
		uni.request({
			url: url,
			method,
			header: headers,
			success(res) {
				// uni.hideLoading();
				resolve(res.data);
			},
			fail: function(res) {
				// uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res);
			}
		});
	})
}
export const courseApi = {
	    // 课程系列管理接口
		/**
		 * @param {page, rows,courseType: ""-全部,0视频,1音频,2图文 }
		 * @description 查询当前专家所有课程系列
		 * @detail 已联调
		 * @author lyy
		 */
		currentExpertCourseList: (data) => requesthttp('POST', `${lk_expert}/expert/course/courseSeries/getCurrentExpertCourseSeriesPageList`, data, {
			'content-type': 'application/json'
		}),
		
		// /**
		//  * @param { courseType }
		//  * @description 带条件分页查询数据
		//  * @detail 已联调
		//  * @author lyy
		//  */
		// coursePagelist: (data) => requesthttp('POST', `${lk_expert}/expert/course/courseSeries/pagelist`, data, {
		// 	'content-type': 'application/json'
		// }),
		
		/**
		 * @param { id }
		 * @description 根据ID获取课程系列信息
		 * @detail 已联调
		 * @author lyy
		 */
		courseSeriesMsg: (id,data) => requesthttp('GET', `${lk_expert}/expert/course/courseSeries/${id}`, data, {
			'content-type': 'application/x-www-form-urlencoded'
		}),
		// 课程管理接口
		
		/**
		 * @param { courseSeriesId }
		 * @description 显示该系列课下的所有课程
		 * @detail 已联调
		 * @author lyy
		 */
		courseByCourseSeriesId: (courseSeriesId,data) => requesthttp('GET', `${lk_expert}/expert/course/course/getCourseByCourseSeriesId/${courseSeriesId}`, data, {
			'content-type': 'application/x-www-form-urlencoded'
		}),
		
		// 课程系列评论管理接口
		/**
		 * @param { seriesId }
		 * @description 获取课程系列评分统计
		 * @detail 已联调
		 * @author lyy
		 */
		EvaluateCount: (seriesId,data) => requesthttp('GET', `${lk_manager}/courseSeriesBaseEvaluate/getEvaluateCount/${seriesId}`, data, {
			'content-type': 'application/x-www-form-urlencoded'
		}),
		/**
		 * @param { ... }
		 * @description 课程系列评论列表
		 * @detail 已联调
		 * @author lyy
		 */
		EvaluateListByPage: (data) => requesthttp('POST', `${lk_manager}/courseSeriesBaseEvaluate/listByPage`, data, {
			'content-type': 'application/json'
		}),
		
		/**
		 * @param { courseSeriesId,pid,evaluateType,expertReply }
		 * @description 更新或添加课程评论
		 * @detail 已联调
		 * @author lyy
		 */
		c_SaveEvaluate: (data) => requesthttp('POST', `${lk_manager}/courseSeriesBaseEvaluate/save`, data, {
			'content-type': 'application/json'
		}),
}
